﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Gem_Tools;
using Gem_Tools_Entity.Gem_Tools;
using Gem_Tools_Controller.Gem_Tools;
using System.Text.RegularExpressions;
using Gem_Tools_Utility;

namespace GE_Management_Tool.Category
{
    public partial class frmA002_Staff_Edit_Form : frmBase, ICategory<CStaff>
    {
        CStaff m_objData;
        Boolean m_isNew = false;
        Boolean m_isChange = false;
        Boolean m_isReset_Pass = false;

        //----------------------------------------------------------------------------------------------
        public frmA002_Staff_Edit_Form()
        {
            InitializeComponent();
            CCommon_Function.Format_All(this);
        }

        public CStaff Show_To_Create()
        {
            this.Text = "Create New Staff Info";
            m_isNew = true;
            this.ShowDialog();

            return m_objData;
        }

        public CStaff Show_To_Edit(CStaff pobjData, ref Boolean pisChange)
        {
            m_isChange = pisChange;
            m_isNew = false;
            m_objData = (CStaff)pobjData.Clone();
            Assign_Obj_To_Control(pobjData);
            this.Text = "Editing " + pobjData.Full_Name + " Info";
            this.ShowDialog();
            pisChange = m_isChange;
            return m_objData;
        }

        public void Assign_Control_To_Obj()
        {
            m_objData.UserName = txtUsername.Text.Trim();
            if (m_isNew == false)
            {
                if (m_isReset_Pass == true)
                {
                    m_objData.Password = CUtility.MD5_Encrypt(txtPassword.Text).ToString();
                }
            }
            else
            {
                m_objData.Password = CUtility.MD5_Encrypt(txtPassword.Text).ToString();
            }
            m_objData.Full_Name = txtFullname.Text.Trim();
            m_objData.Regency_ID = cbbRegency.SelectedIndex;
            m_objData.Email = txtEmail.Text.Trim();
            m_objData.Extension = txtExtension.EditValue.ToString();
            m_objData.Note = txtNote.Text.Trim();
            m_objData.is_Active = CUtility.Convert_To_Int32(chkInactivity.EditValue);
            m_objData.Last_Updated_By = CSession.Active_User_Name;
        }

        public void Assign_Obj_To_Control(CStaff pobjData)
        {
            txtUsername.Text = pobjData.UserName;
            txtRetypePassword.Text = txtPassword.Text = pobjData.Password;
            txtFullname.Text = pobjData.Full_Name;
            cbbRegency.SelectedIndex = pobjData.Regency_ID;
            txtEmail.Text = pobjData.Email;
            txtExtension.Value = int.Parse(pobjData.Extension);
            txtNote.Text = pobjData.Note;
            chkInactivity.EditValue = CUtility.Convert_To_Bool(m_objData.is_Active);
        }

        public string Check_Before_Save()
        {
            StringBuilder v_sb = new StringBuilder();
            if(txtFullname.Text == "")
            {
                v_sb.AppendLine("- Missing Full Name Info.");
            }

            if (txtUsername.Text == "")
            {
                v_sb.AppendLine("- Missing User Name.");
            }

            if (txtPassword.Text == "" || txtRetypePassword.Text == "")
            {
                v_sb.AppendLine("- Missing Password.");
            }

            if (txtPassword.Text 
                != txtRetypePassword.Text)
            {
                v_sb.AppendLine("- 2 Password Not Match.");
            }

            if (cbbRegency.Text == "")
            {
                v_sb.AppendLine("- Missing Member of group info.");
            }

            if (txtEmail.Text != "")
            {
                string email = txtEmail.Text;
                Regex regex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
                Match match = regex.Match(email);
                if (match.Success == false)
                {
                    v_sb.AppendLine("- Email is not correct.");
                }
            }

            return v_sb.ToString();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                m_isChange = true;
                string error = "";
                error = Check_Before_Save();
                if (error != "")
                {
                    throw new Exception(error);
                }

                if (m_isNew == true)
                {
                    m_objData = new CStaff();
                    Assign_Control_To_Obj();
                    CStaff_Controller v_objCtrlStaff = new CStaff_Controller();
                    m_objData.Auto_ID = v_objCtrlStaff.Insert_Staff(m_objData);
                }
                else
                {
                    Assign_Control_To_Obj();
                    CStaff_Controller v_objCtrlStaff = new CStaff_Controller();
                    v_objCtrlStaff.Update_Staff(m_objData);
                }

                XtraMessageBox.Show("Success !");
                this.Close();
            }
            catch (Exception ex)
            {
                m_isChange = false;
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);
            }
        }

        private void txtFullname_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (m_isNew == true)
                {
                    if (txtFullname.Text.Trim().Contains(" ") == false)
                    {
                        txtUsername.Text = txtFullname.Text;
                    }
                    else
                    {
                        string[] temp = txtFullname.Text.Split(' ');
                        txtUsername.Text = temp[temp.Length - 1];
                        for (int i = 0; i < temp.Length - 1; i++)
                        {
                            txtUsername.Text += temp[i][0].ToString().ToUpper();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);
            }
        }

        private void frmA002_Staff_Edit_Form_Load(object sender, EventArgs e)
        {
            if (m_isNew == true)
            {
                chkReset_Password.Enabled = false;
                chkInactivity.Enabled = false;
            }
            else
            {
                txtPassword.Enabled = false;
                txtRetypePassword.Enabled = false;
            }
        }

        private void chkReset_Password_CheckedChanged(object sender, EventArgs e)
        {
            if (m_isNew == false)
            {
                if (chkReset_Password.Checked == true)
                {
                    txtPassword.Enabled = true;
                    txtRetypePassword.Enabled = true;
                    m_isReset_Pass = true;
                    txtPassword.Text = "";
                    txtRetypePassword.Text = "";
                }
                else
                {
                    txtPassword.Enabled = false;
                    txtRetypePassword.Enabled = false;
                    m_isReset_Pass = false;
                    txtPassword.Text = m_objData.Password;
                    txtRetypePassword.Text = m_objData.Password;
                }
            }
        }
    }
}